#include "aky_program/aky_com_include.h"
static void my_event_cb(lv_obj_t * obj, lv_event_t event);
void label_set_stat(uint8_t stat);
void auto_test_stat_pro(void);
uint16_t get_mode_time_max(uint8_t mode_sel);
uint16_t get_mode_step_time(uint8_t mode_sel);
uint16_t get_mode_run_time(uint8_t mode_sel);
uint16_t get_mode_set_pres(uint8_t mode_sel);
void show_err_code_detail(uint8_t err_code);
void down_detect(uint8_t stat);
void cuff_err_detect_init(void);
uint8_t get_cuff_err(uint16_t left_target,uint16_t right_target,uint16_t pump_dir);
void video_set_show_stat(uint8_t stat);
void video_stop(void);



static lv_obj_t *label_title;          //
static lv_obj_t *label_title2;          //"训练中......"
static lv_obj_t *label_left_pres;          //测量时左侧实时压力 训练时左侧设定压力
static lv_obj_t *label_right_pres;          //测量时右侧实时压力 训练时左侧实时压力

static lv_obj_t *label_right_set_pres;          //增加训练时显示右侧设定压力
static lv_obj_t *label_right_pres_2;          //增加训练时显示右侧实时压力
static lv_obj_t *label_tran_title;          //增加训练时显示 左臂 和 右臂 标题

static lv_obj_t *label_heart_rate;          //

static lv_obj_t *tc_area_one_click_stop;          //一键停止
static lv_obj_t *tc_area_ready;          //准备好了
static lv_obj_t *tc_area_back;          //返回
static lv_obj_t *img_start_test;          //开始测量
static lv_obj_t *img_test_stop;          //训练中断
static lv_obj_t *image_sqr_code;          //
static lv_obj_t *tc_area_time;          //
static lv_obj_t *img_notice_high_bp;          //当前血压不适宜进行训练
static lv_obj_t *label_video;          //播放视频

uint8_t flag_start_test = 0;
uint8_t flag_test_stop = 0;
uint8_t flag_retest_bp = 0;
uint8_t flag_retest_bp_done = 0;
static uint16_t test_total_run_time = 0;
static uint16_t test_total_run_time_last = 0;

e_auto_test_stat_t e_auto_test_stat = STAT_CAL_ZERO;

stru_aky_task_0_t s_aky_task_0 = {0};
stru_aky_task_1_t s_aky_task_1 = {0};
static uint16_t test_time_max = 0;
s_mem_t s_mem_first_test = {0};
uint16_t run_time_mem = 0;
static uint16_t down_cnt_left; //反复压力波动 次数
static uint16_t down_cnt_right;


//视频播放
static lv_obj_t *avi_2;          //
static lv_obj_t *image_close;          //
static lv_obj_t *touch_area_avi_1;          //
static lv_obj_t *touch_area_avi_2;          //
static lv_obj_t *touch_area_avi_3;          //
static lv_obj_t *touch_area_avi_4;          //
static uint8_t flag_start_play_avi = 0;
static lv_obj_t *tc_area_back_video;          //
static lv_obj_t *bar_indic_voice;          //
static lv_obj_t *bar_indic_bl;          //
static lv_obj_t *image_video_bg;          //

static uint8_t flag_disp_bar = 0;
static uint16_t video_index = 0;

extern const char *video_str_table[];
static uint8_t flag_one_stop_trig = 0;

static uint8_t flag_is_open_video = 0;


void video_screen_on_timer(void)
{
    /* 界面定时器回调函数 */
    static uint16_t time_cnt = 0;
    static uint16_t fil_cnt = 0;
    static uint16_t flag_disp_bar_onece = 0;
    static uint16_t time_cnt2 = 0;
    static uint16_t time_cnt3 = 0;

    if (flag_is_open_video == 0)
    {
        if (get_spk_recogni_info() == WATCH_VIDEO_1)
        {
            clr_spk_recogni_info();
        }
        if (get_spk_recogni_info() == WATCH_VIDEO_2)
        {
            clr_spk_recogni_info();
        }
        if (get_spk_recogni_info() == WATCH_VIDEO_3)
        {
            clr_spk_recogni_info();
        }
        if (get_spk_recogni_info() == WATCH_VIDEO_4)
        {
            clr_spk_recogni_info();
        }
        if (get_spk_recogni_info() == WATCH_VIDEO_5)
        {
            clr_spk_recogni_info();
        }
        if (get_spk_recogni_info() == WATCH_VIDEO_6)
        {
            clr_spk_recogni_info();
        }
    
        return;
    }
    
    if (g_flag_video_delay_play == 1 && flag_start_play_avi == 1)
    {
        time_cnt3++;
        if (time_cnt3 >= 300 / 10)
        {
            g_flag_video_delay_play = 0;
            time_cnt3 = 0;
            my_dac_open();
        }
    }
    else
    {
        time_cnt3 = 0;
    }
    if (flag_one_stop_trig == 1 && flag_start_play_avi == 0)
    {
        time_cnt2++;
        if (time_cnt2 >= 300 / 10)
        {
            flag_one_stop_trig = 0;
            time_cnt2 = 0;
            lv_avi_stop(avi_2);
        }
    }
    else
    {
        time_cnt2 = 0;
    }


    if (flag_disp_bar == 1)
    {
        flag_disp_bar = 0;
        flag_disp_bar_onece = 1;
        time_cnt = 0;
    }
    if (flag_disp_bar_onece == 1)
    {
        time_cnt++;
        if (time_cnt > 500 / 10)
        {
            time_cnt = 0;
            flag_disp_bar_onece = 0;
            lv_obj_set_hidden(bar_indic_voice,true);
            lv_obj_set_hidden(bar_indic_bl,true);
        }
    }
    // if (get_spk_recogni_info() == SPK_BACK)
    // {
    //     clr_spk_recogni_info();
    //     lv_avi_pause(avi_2);
    // 	stop_harware_dac();
    //     lv_obj_set_hidden(avi_2, true);
    //     lv_obj_set_hidden(image_close, true);
    //     flag_start_play_avi = 0;
    //     lv_obj_set_click(avi_2,false);

    //     stat_bar_restart_flush();

    //     synwit_ui_load_screen(SCREEN005);        
    // }

    if (flag_start_play_avi == 1)
    {
        off_time_clr();
        if (g_s_set.voice_level > 0)
        {
            my_dac_open();
        }
        avi_state_t avi_state = lv_avi_get_state(avi_2);
        if (avi_state == AVI_STATE_STOPPED)
        {
            fil_cnt++;
            if (fil_cnt > 1000 / 10)
            {
                fil_cnt = 0;
                if (lv_avi_set_src(avi_2, video_str_table[video_index]) == 0)
                {
                    printf("lv_avi_replay\n");
                    lv_avi_replay(avi_2);
                    dac_init(DAC_FORMAT_MSB12B);
                    my_dac_open();

                }
            }
        }
        else
        {
            fil_cnt = 0;
        }

    }
    if (get_spk_recogni_info() == WATCH_VIDEO_1)
    {
        clr_spk_recogni_info();
        lv_event_send(touch_area_avi_1,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == WATCH_VIDEO_2)
    {
        clr_spk_recogni_info();
        lv_event_send(touch_area_avi_2,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == WATCH_VIDEO_3)
    {
        clr_spk_recogni_info();
        lv_event_send(touch_area_avi_3,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == WATCH_VIDEO_4)
    {
        clr_spk_recogni_info();
        lv_event_send(touch_area_avi_4,LV_EVENT_CLICKED,NULL);
    }

    if (get_spk_recogni_info() == BL_UP || get_spk_recogni_info() == BL_DOWN)
    {
        clr_spk_recogni_info();
        if (flag_start_play_avi == 1)
        {
            lv_obj_set_hidden(bar_indic_bl,false);
            flag_disp_bar = 1;
            lv_bar_set_value(bar_indic_bl,g_s_set.bl_level * BL_LEVEL_STEP_VAL,LV_ANIM_OFF);
        }
    }
    if (get_spk_recogni_info() == VOICE_UP || get_spk_recogni_info() == VOICE_DOWN || get_spk_recogni_info() == VOICE_SHUT)
    {
        clr_spk_recogni_info();
        if (flag_start_play_avi == 1)
        {
            lv_obj_set_hidden(bar_indic_voice,false);
            flag_disp_bar = 1;
            if (g_s_set.voice_level * VOICE_LEVEL_STEP_VAL < 10)
            {
                lv_bar_set_value(bar_indic_voice,5,LV_ANIM_OFF);
            }
            else
            {
                lv_bar_set_value(bar_indic_voice,g_s_set.voice_level * VOICE_LEVEL_STEP_VAL,LV_ANIM_OFF);
            }
            lv_avi_set_volume(avi_2, get_video_voice_val(g_s_set.voice_level), AVI_VOLUME_ADJ_MODE_ABSOLUTE);
            if (g_s_set.voice_level == 0)
            {
                stop_harware_dac();
                g_flag_video_delay_play = 0;
            }
            else
            {
                my_dac_open();
            }

        }
    } 

}
void video_screen_start(void)
{
    flag_start_play_avi = 0;
    flag_disp_bar = 0;
    video_index = 0;
    flag_one_stop_trig = 0;

    avi_2 = synwit_ui_find_lv_obj(WIDGET_AVI_3);

    image_close = synwit_ui_find_lv_obj(WIDGET_IMAGE34);

    touch_area_avi_1 = synwit_ui_find_lv_obj(WIDGET_TOUCH_39);
    touch_area_avi_2 = synwit_ui_find_lv_obj(WIDGET_TOUCH_40);
    touch_area_avi_3 = synwit_ui_find_lv_obj(WIDGET_TOUCH_41);
    touch_area_avi_4 = synwit_ui_find_lv_obj(WIDGET_TOUCH_17);

    lv_obj_set_click(image_close,true);
    lv_obj_set_ext_click_area(image_close, 50, 0, 0, 50);

    tc_area_back_video = synwit_ui_find_lv_obj(WIDGET_TOUCH_43);
    bar_indic_voice = synwit_ui_find_lv_obj(WIDGET_BAR7);
    bar_indic_bl = synwit_ui_find_lv_obj(WIDGET_BAR8);
    image_video_bg = synwit_ui_find_lv_obj(WIDGET_IMAGE30);

    lv_bar_set_value(bar_indic_voice,g_s_set.voice_level * VOICE_LEVEL_STEP_VAL,LV_ANIM_OFF);
    lv_bar_set_value(bar_indic_bl,g_s_set.bl_level * BL_LEVEL_STEP_VAL,LV_ANIM_OFF);

    lv_obj_set_hidden(bar_indic_voice, true);
    lv_obj_set_hidden(bar_indic_bl, true);
    lv_obj_set_event_cb(touch_area_avi_1, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(touch_area_avi_2, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(touch_area_avi_3, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(touch_area_avi_4, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(image_close, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_back_video, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(avi_2, my_event_cb);   /*Assign an event callback*/
}

void video_screen_stop(void)
{
    g_flag_video_delay_play = 0;

}
static void my_avi_complete_cb(lv_obj_t* avi, void *user_data)
{
    printf("screen8 my_avi_complete_cb\n");
    lv_avi_pause(avi_2);
	stop_harware_dac();
    lv_obj_set_hidden(avi_2, true);
    lv_obj_set_hidden(image_close, true);
    flag_start_play_avi = 0;
    lv_obj_set_click(avi_2,false);
    stat_bar_restart_flush();
}

static void video_screen_event_cb(lv_obj_t * obj, lv_event_t event)
{
    if (flag_start_play_avi == 1)
    {
        if (obj == image_close)
        {
            if (event == LV_EVENT_CLICKED)
            {
                // synwit_ui_load_screen(SCREEN004);
                printf("screen8 image_close\n");

                lv_avi_pause(avi_2);
            	stop_harware_dac();
                flag_one_stop_trig = 1;
                lv_obj_set_hidden(avi_2, true);
                lv_obj_set_hidden(image_close, true);
                flag_start_play_avi = 0;
                g_flag_video_delay_play = 0;

                lv_obj_set_click(avi_2,false);
                lv_obj_set_hidden(bar_indic_voice,true);
                lv_obj_set_hidden(bar_indic_bl,true);

                stat_bar_restart_flush();
            }
        }
        else if (obj == avi_2)
        {
            static lv_point_t point_start;
            static lv_point_t point_end;
            if (event == LV_EVENT_PRESSED)
            {
                lv_indev_get_point(lv_indev_get_act(),&point_start);
                printf("LV_EVENT_PRESSED point_start x=%d y=%d \n",point_start.x,point_start.y);
            }
            if (event == LV_EVENT_RELEASED)
            {
                lv_indev_get_point(lv_indev_get_act(),&point_end);
                printf("LV_EVENT_RELEASED point_end x=%d y=%d \n",point_end.x,point_end.y);
                s_slide_info_t s_slide_info = get_slide_info(point_start,point_end);
                printf("slide_info.area=%d slide_info.dir=%d slide_info.strength=%d\n",s_slide_info.area,s_slide_info.dir,s_slide_info.strength);
                int16_t bar_val;
                if (s_slide_info.area == area_right)
                {
                    bar_val = lv_bar_get_value(bar_indic_voice);
                    if (s_slide_info.dir == dir_up)
                    {
                        bar_val += s_slide_info.strength * VOICE_LEVEL_STEP_VAL;
                        if (bar_val > 100)
                        {
                            bar_val = 100;
                        }
                    }
                    else
                    {
                        bar_val -= s_slide_info.strength * VOICE_LEVEL_STEP_VAL;
                        if (bar_val <= 0)
                        {
                            bar_val = 0;
                        }
                    }
                    if (bar_val <= 5)
                    {
                        lv_bar_set_value(bar_indic_voice,5,LV_ANIM_OFF);
                    }
                    else
                    {
                        lv_bar_set_value(bar_indic_voice,bar_val,LV_ANIM_OFF);
                    }
                    lv_obj_set_hidden(bar_indic_voice,false);
                    flag_disp_bar = 1;
                    g_s_set.voice_level = get_voice_level(bar_val);
                    lv_avi_set_volume(avi_2, get_video_voice_val(g_s_set.voice_level), AVI_VOLUME_ADJ_MODE_ABSOLUTE);
                    if (g_s_set.voice_level == 0)
                    {
                        stop_harware_dac();
                        g_flag_video_delay_play = 0;
                    }
                    else
                    {
                        my_dac_open();
                    }
                    printf("voice_level:%d\n",g_s_set.voice_level);

                }
                else if (s_slide_info.area == area_left)
                {
                    bar_val = lv_bar_get_value(bar_indic_bl);
                    if (s_slide_info.dir == dir_up)
                    {
                        bar_val += s_slide_info.strength * BL_LEVEL_STEP_VAL;
                        if (bar_val > 100)
                        {
                            bar_val = 100;
                        }
                    }
                    else
                    {
                        bar_val -= s_slide_info.strength * BL_LEVEL_STEP_VAL;
                        if (bar_val <= 0)
                        {
                            bar_val = 0;
                        }
                    }
                    if (bar_val <= 5)
                    {
                        lv_bar_set_value(bar_indic_bl,5,LV_ANIM_OFF);
                    }
                    else
                    {
                        lv_bar_set_value(bar_indic_bl,bar_val,LV_ANIM_OFF);
                    }
                    lv_obj_set_hidden(bar_indic_bl,false);
                    flag_disp_bar = 1;
                    g_s_set.bl_level = get_bl_level(bar_val);
                    set_bl_by_level(g_s_set.bl_level);
                    printf("bl_level:%d\n",g_s_set.bl_level);
                }
                
                
            }
            
        }     
        return;
    }
    if (obj == touch_area_avi_1 || obj == touch_area_avi_2 || obj == touch_area_avi_3 || obj == touch_area_avi_4)
    {
        if (event == LV_EVENT_CLICKED)
        {
            // synwit_ui_load_screen(SCREEN004);
            if (obj == touch_area_avi_1)
            {
                video_index = 0;
            }
            else if (obj == touch_area_avi_2)
            {
                video_index = 1;
            }
            else if (obj == touch_area_avi_3)
            {
                video_index = 2;
            }
            else if (obj == touch_area_avi_4)
            {
                video_index = 3;
            }


            
            if (lv_avi_set_src(avi_2, video_str_table[video_index]) == 0)
            {
                flag_start_play_avi = 1;
                g_flag_video_delay_play = 1;
                lv_avi_replay(avi_2);
                lv_obj_set_hidden(avi_2, false);
                lv_obj_set_hidden(image_close, false);
                lv_avi_set_volume(avi_2, get_video_voice_val(g_s_set.voice_level), AVI_VOLUME_ADJ_MODE_ABSOLUTE);
                if (g_s_set.voice_level == 0)
                {
                    stop_harware_dac();
                    g_flag_video_delay_play = 0;
                }
                else
                {
                    my_dac_open();
                }
                dac_init(DAC_FORMAT_MSB12B);
                lv_obj_set_click(avi_2,true);
                stat_bar_stop_flush(0);
                lv_avi_set_on_completed_cb(avi_2,my_avi_complete_cb, NULL);
            }

        }   
    }
    if (obj == tc_area_back_video)
    {
        if (event == LV_EVENT_CLICKED)
        {
            // synwit_ui_load_screen(SCREEN005);
            flag_is_open_video = 0;
            video_set_show_stat(0);
            video_set_show_stat(3);

        }
    }

}
/*
 * 界面回调函数
 */
static void screen008_on_timer(void *user_data)
{
    /* 界面定时器回调函数 */
    screen_flush_pro();
    video_screen_on_timer();

    {
        auto_test_stat_pro();
    }
    if (get_spk_recogni_info() == START_THIS_TRAIN)
    {
        clr_spk_recogni_info();
        lv_event_send(tc_area_ready,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == ONE_CLICK_STOP)
    {
        clr_spk_recogni_info();
        if (flag_test_stop == 0)
        {
            lv_event_send(tc_area_one_click_stop,LV_EVENT_CLICKED,NULL);
        }
    }
}

static void screen008_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */
    flag_start_test = 0;
    flag_test_stop = 0;
    flag_retest_bp = 0;
    flag_retest_bp_done = 0;
    e_auto_test_stat = STAT_CAL_ZERO;

    video_screen_start();
    memset((uint8_t *)&s_mem_first_test,0,sizeof(s_mem_t));

    label_title = synwit_ui_find_lv_obj(WIDGET_LABEL16);
    label_left_pres = synwit_ui_find_lv_obj(WIDGET_LABEL17);
    label_right_pres = synwit_ui_find_lv_obj(WIDGET_LABEL18);
    tc_area_one_click_stop = synwit_ui_find_lv_obj(WIDGET_TOUCH_21);
    tc_area_ready = synwit_ui_find_lv_obj(WIDGET_TOUCH_19);
    tc_area_back = synwit_ui_find_lv_obj(WIDGET_TOUCH_20);
    img_start_test = synwit_ui_find_lv_obj(WIDGET_IMAGE14);
    img_test_stop = synwit_ui_find_lv_obj(WIDGET_IMAGE15);
    label_heart_rate = synwit_ui_find_lv_obj(WIDGET_LABEL20);
    label_right_pres_2 = synwit_ui_find_lv_obj(WIDGET_LABEL19);
    label_title2 = synwit_ui_find_lv_obj(WIDGET_LABEL22);
    tc_area_time = synwit_ui_find_lv_obj(WIDGET_TOUCH_37);
    img_notice_high_bp = synwit_ui_find_lv_obj(WIDGET_IMAGE23);
    label_right_set_pres = synwit_ui_find_lv_obj(WIDGET_LABEL37);
    label_tran_title = synwit_ui_find_lv_obj(WIDGET_LABEL36);
    // img_video = synwit_ui_find_lv_obj(WIDGET_IMAGE35);
    label_video = synwit_ui_find_lv_obj(WIDGET_LABEL62);

    lv_label_set_text_fmt(label_title2,"训练中...");

    // image_sqr_code = synwit_ui_find_lv_obj(WIDGET_IMAGE25);
    // lv_obj_set_parent(image_sqr_code,img_start_test);
	lv_font_t* font_temp = synwit_ui_font_load("S:spi:wqyMicroHeiMono_40.hmf", false);
	lv_obj_set_style_local_text_font(label_right_pres, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, font_temp);	

    lv_obj_set_hidden(label_title,true);
    lv_obj_set_hidden(label_left_pres,true);
    lv_obj_set_hidden(label_right_pres,true);
    lv_obj_set_hidden(img_start_test, true);
    lv_obj_set_hidden(img_test_stop, true);
    lv_obj_set_hidden(label_heart_rate, true);
    lv_obj_set_hidden(label_right_pres_2, true);
    lv_obj_set_hidden(img_notice_high_bp, true);
    lv_obj_set_hidden(label_right_set_pres, true);
    lv_obj_set_hidden(label_tran_title, true);

    lv_label_set_text_fmt(label_left_pres,"左侧:  0");
    lv_label_set_text_fmt(label_right_pres,"右侧:  0");
    lv_obj_set_click(label_video,true);

    label_set_stat(0);
    video_set_show_stat(0);

    #if (DEBUG_PREADAP_SKIP == 1)
    lv_obj_set_hidden(tc_area_time, false);
    lv_obj_set_event_cb(tc_area_time, my_event_cb);   /*Assign an event callback*/

    #else
    lv_obj_set_hidden(tc_area_time, true);

    #endif

    lv_obj_set_event_cb(tc_area_one_click_stop, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_ready, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_back, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_video, my_event_cb);   /*Assign an event callback*/
    if (check_low_bat_pro(LOW_BAT_THRESHOLD) == 1)
    {
        g_flag_low_bat = 1;
        synwit_ui_load_screen(SCREEN013);
    }
    

    if (g_flag_emc_mode == 1)
    {
        flag_start_test = 1;
        lv_obj_set_hidden(img_start_test, false);
        label_set_stat(1);
    }
    
    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    screen_init();
    synwit_ui_start_scr_timer(10, NULL);
}

static void screen008_stop(synwit_id_t screen_id)
{
    /* 准备切换到其它界面前，这个接口会被调用。 */
    screen_stop();
    video_screen_stop();
    voice_exit_bg_play();
    aky_task_0_send_cmd(TASK0_BP_STOP_Command);
    aky_task_1_send_cmd(TASK1_BP_STOP_Command);
    g_flag_emc_mode = 0;
}

static int screen008_favorite_stop_policy()
{
    /* 函数返回值表示本界面结束时，希望采用的资源管理策略 */

    /* 资源管理策略(主要影响本界面再次进入时的加载速度):
     *    0: 系统推荐，释放当前界面引用的资源，保留控件及控件状态。可获得平衡的界面切换性能和内存消耗
     *    1: 释放当前界面引用的资源，并删除所有控件。
     *    2: 保留当前界面的所有资源及控件。可以获得最佳的界面切换性能，但内存消耗较大
     */
    return 0;
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen008_cb_obj = {
    .favorite_stop_policy = screen008_favorite_stop_policy,
    .on_screen_start = screen008_start,
    .on_screen_stop = screen008_stop,
    .on_screen_timer = screen008_on_timer,
};

static void my_event_cb(lv_obj_t * obj, lv_event_t event)
{
    off_time_clr();
    if(flag_is_open_video == 1)
    {
        video_screen_event_cb(obj,event);
        if (obj == tc_area_one_click_stop)
        {
            if (event == LV_EVENT_CLICKED)
            {
                if (flag_test_stop == 0)
                {
                    flag_test_stop = 1;
                    if (flag_retest_bp_done == 1)
                    {
                        voice_stop();
                        voice_exit_bg_play();  
                        synwit_ui_load_screen(SCREEN005);
                    }
                    else
                    {
                        e_auto_test_stat = STAT_MANUAL_STOP;
                    }
                }
                else
                {
                    if (flag_manual_pre_adapt == 1)
                    {   
                        synwit_ui_load_screen(SCREEN010);
                    }
                    else
                    {
                        synwit_ui_load_screen(SCREEN005);
                    }
                }
            }
        }

        return;
    }

#if (DEBUG_PREADAP_SKIP == 1)
    if (obj == tc_area_time)
    {
        if (event == LV_EVENT_PRESSING)
        {
            // aky_voice_table_play(MEASURE_BEGIN);
            if(e_auto_test_stat == STAT_CAL_ZERO)
            {
                e_auto_test_stat = STAT_SHOW_ERR;
            }
            if (test_total_run_time > 10)
            {
                test_total_run_time-=5;
            }
        }
    }
#endif
    if (flag_start_test == 1)
    {
        if (obj == tc_area_one_click_stop)
        {
            if (event == LV_EVENT_CLICKED)
            {
                if (flag_test_stop == 0)
                {
                    flag_test_stop = 1;
                    if (flag_retest_bp_done == 1)
                    {
                        voice_stop();
                        voice_exit_bg_play();  
                        synwit_ui_load_screen(SCREEN005);
                    }
                    else
                    {
                        e_auto_test_stat = STAT_MANUAL_STOP;
                    }
                }
                else
                {
                    if (g_flag_emc_mode == 1)
                    {
                        //返回工程界面
                        g_flag_emc_mode = 0;
                        synwit_ui_load_screen(SCREEN012);
                    }
                    else
                    {
                        if (flag_manual_pre_adapt == 1)
                        {   
                            synwit_ui_load_screen(SCREEN010);
                        }
                        else
                        {
                            synwit_ui_load_screen(SCREEN005);
                        }
                    }

                }
            }
        }
        if (obj == label_video)
        {
            if (event == LV_EVENT_CLICKED)
            {
                flag_is_open_video = 1;
                video_set_show_stat(1);
                voice_stop();

            }
        }
        
        return;
    }
    
    if (obj == tc_area_ready)
    {
        if (event == LV_EVENT_CLICKED)
        {
            // aky_voice_table_play(MEASURE_BEGIN);
            flag_start_test = 1;
            e_auto_test_stat = STAT_CAL_ZERO;
        }
    }
    if (obj == tc_area_back)
    {
        if (event == LV_EVENT_CLICKED)
        {
            if (flag_manual_pre_adapt == 1)
            {
                synwit_ui_load_screen(SCREEN010);
            }
            else
            {
                synwit_ui_load_screen(SCREEN005);
            }
        }
    }
}
//e_aky_task_2_stat_t e_aky_task_2_stat = TASK_2_STAT_ZERO_END;
//e_aky_task_2_stat_t get_bp1_stat(void)
//{
    //return e_aky_task_2_stat;
//}

void auto_test_stat_pro(void)
{
    static uint16_t pres_left = 0;
    static uint16_t pres_right = 0;
    static uint8_t second_last = 0;
    static uint8_t second_last_2 = 0;
    static uint8_t err_code = 0;
    static uint16_t step_sub = 0;
    static uint8_t zero_flash_stat = 0;
    static uint16_t pres_sys_last = 0;
    static uint16_t pres_sys_last2 = 0;
    static uint16_t pres_set_left = 0;
    static uint16_t pres_set_right = 0;
    static uint8_t flag_update_right = 0;
    static uint8_t flag_update_left = 0;
    static uint8_t flag_update_onece = 0;
    static uint8_t flag_start_update = 0;

    static uint16_t fil_cnt = 0;
    static uint16_t fil_cnt2 = 0;
    static uint16_t send_cnt2= 0;
    static uint16_t fil_cnt4= 0;
    static uint16_t send_cnt = 0;
    static uint32_t u32_tick_last = 0;
    static uint32_t u32_tick_last2 = 0;
    static uint16_t tran_step = 0;

    static uint16_t left_show_pres_last = 0;
    static uint16_t right_show_pres_last = 0;

    s_mem_t s_mem_last = {0};

    static e_auto_test_stat_t e_auto_test_stat_last = STAT_UNDEF;
    if (check_low_bat_trig(LOW_BAT_THRESHOLD - 20) && ((e_auto_test_stat == STAT_PRE_ADAPT && step_sub >= 2) || e_auto_test_stat == STAT_TESTING_BP))
    {
        video_stop();
        printf("screen8 check_low_bat_trig\n");

        lv_obj_set_hidden(img_test_stop, false);
        label_set_stat(5);
        voice_stop();
        voice_exit_bg_play();   
               if (flag_retest_bp == 1 || s_mem_first_test.s_left_bp.sys != 0)
                {
                    if (flag_retest_bp == 1)
                    {
                        if (flag_manual_pre_adapt == 1)
                        {
                            run_time_mem = get_mode_time_max(pre_adapt_mode_sel) - test_total_run_time;
                        }
                        else
                        {
                            run_time_mem = AUTO_TIME_MAX - test_total_run_time;
                        }
                    }
                    else
                    {
                        if (flag_manual_pre_adapt == 1)
                        {
                            run_time_mem = (get_mode_run_time(pre_adapt_mode_sel) - test_total_run_time);
                        }
                        else
                        {
                            run_time_mem = (AUTO_RUN_TIME - test_total_run_time);
                        }
                    }
                    memset((uint8_t *)&g_s_mem,0,sizeof(s_mem_t));
                    g_s_mem = s_mem_first_test;

                    g_s_mem.s_train_time.min = run_time_mem/60;
                    g_s_mem.s_train_time.secon = run_time_mem%60;
                    insert_one_mem(g_s_mem,g_e_user);
                    // syn_mem_to_net_control(1);
                }

        aky_task_0_send_cmd(TASK0_BP_STOP_Command);
        aky_task_1_send_cmd(TASK1_BP_STOP_Command);

        g_flag_low_bat = 1;
        synwit_ui_load_screen(SCREEN013);

    }
    if (get_spk_recogni_info() == SPK_BACK)
    {
        clr_spk_recogni_info();
        video_stop();
        printf("screen8 get_spk_recogni_info() == SPK_BACK\n");

        lv_obj_set_hidden(img_test_stop, false);
        label_set_stat(5);
        voice_stop();
        voice_exit_bg_play();   
               if (flag_retest_bp == 1 || s_mem_first_test.s_left_bp.sys != 0)
                {
                    if (flag_retest_bp == 1)
                    {
                        if (flag_manual_pre_adapt == 1)
                        {
                            run_time_mem = get_mode_time_max(pre_adapt_mode_sel) - test_total_run_time;
                        }
                        else
                        {
                            run_time_mem = AUTO_TIME_MAX - test_total_run_time;
                        }
                    }
                    else
                    {
                        if (flag_manual_pre_adapt == 1)
                        {
                            run_time_mem = (get_mode_run_time(pre_adapt_mode_sel) - test_total_run_time);
                        }
                        else
                        {
                            run_time_mem = (AUTO_RUN_TIME - test_total_run_time);
                        }
                    }
                    memset((uint8_t *)&g_s_mem,0,sizeof(s_mem_t));
                    g_s_mem = s_mem_first_test;

                    g_s_mem.s_train_time.min = run_time_mem/60;
                    g_s_mem.s_train_time.secon = run_time_mem%60;
                    insert_one_mem(g_s_mem,g_e_user);
                    syn_mem_to_net_control(1);
                }

        aky_task_0_send_cmd(TASK0_BP_STOP_Command);
        aky_task_1_send_cmd(TASK1_BP_STOP_Command);
        synwit_ui_load_screen(SCREEN005);  
    }
    if (flag_start_test == 0)
    {
        test_time_max = 0;
        return;
    }

    if (e_auto_test_stat_last != e_auto_test_stat)
    {
        e_auto_test_stat_last = e_auto_test_stat;
        step_sub = 0;
    }
    
    if (e_auto_test_stat < STAT_SHOW_BP_RESULT)
    {
        if (second_last_2 != g_s_time.secon)
        {
            second_last_2 = g_s_time.secon;
            test_time_max++;
        }
        if (test_time_max > 180)
        {
            err_code = 1;
            e_auto_test_stat = STAT_SHOW_ERR;
            printf("test_time_max > 180\n");

        }
    }
    else
    {
        test_time_max = 0;
    }
    
    if (e_auto_test_stat < STAT_SHOW_ERR && flag_retest_bp_done == 0)
    {
        off_time_clr();
    }
    s_aky_task_0 = aky_task_0_get_data();
    s_aky_task_1 = aky_task_1_get_data();
    switch (e_auto_test_stat)
    {
    case STAT_CAL_ZERO:
    {
        if (step_sub == 0)
        {
            step_sub++;
            voice_exit_bg_play();
            lv_obj_set_hidden(img_start_test, false);
            label_set_stat(1);
            printf("start_test\r\n");
            voice_play_single(MEASURE_BEGIN);
            test_time_max = 0;
            err_code = 2;
            pres_sys_last = 0;
            pres_sys_last2 = 0;
            test_total_run_time = 0;
            send_cnt = 0;
            u32_tick_last = g_u32_tick_cnt;
            clr_task_0_data();
            clr_task_1_data();
            if (flag_retest_bp == 0)
            {
                pres_set_left = 0;
            }
            
            pres_set_right = 0;
            flag_update_right = 0;
            flag_update_left = 0;
            flag_update_onece = 0;
            flag_start_update = 0;
            tran_step = 1;
        }
        if (step_sub == 1)
        {
            if (g_u32_tick_cnt - u32_tick_last > 300 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                if (get_voice_busy_stat() == 0)
                {
            #if (DEBUG_PREADAP == 1)

            #else
                aky_task_0_send_cmd(TASK0_BP_READY_Command);
                aky_task_1_send_cmd(TASK1_BP_READY_Command);
            #endif
                    send_cnt++;
                    if (send_cnt >= 3)
                    {
                        send_cnt = 0;
                        step_sub = 3;
                        test_time_max = 0;
                        err_code = 2;
                        pres_sys_last = 0;
                        pres_sys_last2 = 0;
                        test_total_run_time = 0;
                        if (flag_retest_bp == 0)
                        {
                            memset((uint8_t *)&s_mem_first_test,0,sizeof(s_mem_t));
                        }
                    }
                    
                    
                }
            }
        }
        else if (step_sub == 3)
        {
            if (g_u32_tick_cnt - u32_tick_last > 300 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
            #if (DEBUG_PREADAP == 1)

            #else
                aky_task_0_send_cmd(TASK0_BP_READY_Command);
                aky_task_1_send_cmd(TASK1_BP_READY_Command);
            #endif
                if (send_cnt >= 40)
                {
                    send_cnt = 0;
                    err_code = 1;
                    e_auto_test_stat = STAT_SHOW_ERR;
                    printf("TASK0_BP_READY_Command send_cnt >= 40\r\n");
                }
            }
            if (s_aky_task_0.e_aky_task_0_stat >= TASK_0_STAT_TESTING && s_aky_task_1.e_aky_task_1_stat >= TASK_1_STAT_TESTING)
            {
                e_auto_test_stat = STAT_TESTING_BP;
            }
#if (DEBUG_PREADAP == 1)
            if (test_time_max > 2)
            {
                e_auto_test_stat = STAT_SHOW_BP_RESULT;
            }
#endif

            if(test_time_max > 15)
            {
                e_auto_test_stat = STAT_SHOW_ERR;
                err_code = 1;
                printf("test_time_max > 10\n");


            }
            if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                 
                if (zero_flash_stat == 1)
                {
                    zero_flash_stat = 0;
                    lv_label_set_text_fmt(label_left_pres,"左侧:  0");
                    lv_label_set_text_fmt(label_right_pres,"右侧:  0");

                }
                else
                {
                    zero_flash_stat = 1;
                    lv_label_set_text_fmt(label_left_pres,"左侧:  ");
                    lv_label_set_text_fmt(label_right_pres,"右侧:  ");
                }
            }        
        }
        
        break;
    }
    case STAT_TESTING_BP:
    {
        if (step_sub == 0)
        {
            step_sub++;
            pres_left = 0;
            pres_right = 0;
            send_cnt = 0;
            test_time_max = 0;
        }
        else if (step_sub == 1)
        {
            if (g_u32_tick_cnt - u32_tick_last > 300 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                aky_task_0_send_cmd(TASK0_BP_START_Command);
                aky_task_1_send_cmd(TASK1_BP_START_Command);
                send_cnt++;
                if (send_cnt >= 10)
                {
                    send_cnt = 0;
                    step_sub = 2;
                }
                if (Task0_BP.flag_reply == 1 && Task1_BP.flag_reply)
                {
                    Task0_BP.flag_reply = 0;
                    Task1_BP.flag_reply = 0;
                    step_sub = 2;
                }
            }

        }
        else if (step_sub == 2)
        {
            if (g_u32_tick_cnt - u32_tick_last > 1000 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                pres_left = s_aky_task_0.pres_mmg;
                pres_right = s_aky_task_1.pres_mmg;
                if (s_aky_task_0.e_aky_task_0_stat == TASK_0_STAT_TEST_SUCC)
                {
                    pres_left = 0;
                }
                if (s_aky_task_1.e_aky_task_1_stat == TASK_1_STAT_TEST_SUCC)
                {
                    pres_right = 0;
                }
                
                lv_label_set_text_fmt(label_left_pres,"左侧:  %d",pres_left);
                lv_label_set_text_fmt(label_right_pres,"右侧:  %d",pres_right);
            }
            if (s_aky_task_0.e_aky_task_0_stat == TASK_0_STAT_TEST_SUCC && s_aky_task_1.e_aky_task_1_stat == TASK_1_STAT_TEST_SUCC)
            {
                e_auto_test_stat = STAT_SHOW_BP_RESULT;
            }
            if (s_aky_task_0.e_aky_task_0_stat == TASK_0_STAT_TEST_FAIL || s_aky_task_1.e_aky_task_1_stat == TASK_1_STAT_TEST_FAIL)
            {
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("STAT_TESTING_BP -> STAT_SHOW_ERR\n");

                if (s_aky_task_0.e_aky_task_0_stat == TASK_0_STAT_TEST_FAIL)
                {
                    err_code = s_aky_task_0.ERRORCode;
                    printf("TASK_0_STAT_PER_FAIL err_code =0x%02d\n",err_code);

                }
                else if (s_aky_task_1.e_aky_task_1_stat == TASK_1_STAT_TEST_FAIL)
                {
                    err_code = s_aky_task_1.ERRORCode;
                    printf("TASK_1_STAT_PER_FAIL err_code =0x%02d\n",err_code);
                }
            }
            
        }
        break;
    }    
    case STAT_SHOW_BP_RESULT:
    {
        if (step_sub == 0)
        {
            step_sub++;
#if (DEBUG_PREADAP == 1)
            s_aky_task_0.pres_sys = 140;
            s_aky_task_0.pres_dia = 111;
            s_aky_task_0.pulse = MAX_PUL;
             s_aky_task_1.pres_sys = 110;
             s_aky_task_1.pres_dia = MAX_DIA;
             s_aky_task_1.pulse = MIN_PUL;
             
#endif

            pres_sys_last = s_aky_task_0.pres_sys;
            pres_sys_last2 = s_aky_task_1.pres_sys;
            if (flag_manual_pre_adapt == 1)
            {
                test_total_run_time = get_mode_run_time(pre_adapt_mode_sel);
            }
            else
            {
                test_total_run_time = AUTO_RUN_TIME;
            }

            s_mem_last = s_mem_first_test;
            memset((uint8_t *)&s_mem_first_test,0,sizeof(s_mem_t));

            s_mem_first_test.s_time = g_s_time;

            s_mem_first_test.s_left_bp.sys = s_aky_task_0.pres_sys;
            s_mem_first_test.s_left_bp.dia = s_aky_task_0.pres_dia;
            s_mem_first_test.s_left_bp.pul = s_aky_task_0.pulse;

            s_mem_first_test.s_right_bp.sys = s_aky_task_1.pres_sys;
            s_mem_first_test.s_right_bp.dia = s_aky_task_1.pres_dia;
            s_mem_first_test.s_right_bp.pul = s_aky_task_1.pulse;

            s_mem_first_test.s_train_time.min = 0;
            s_mem_first_test.s_train_time.secon = 0;

            if (flag_manual_pre_adapt == 1)
            {
                s_mem_first_test.TrainMode = pre_adapt_mode_sel;
                s_mem_first_test.TrainPressure = get_mode_set_pres(pre_adapt_mode_sel);
            }
            else
            {
                s_mem_first_test.TrainMode = 5;
                s_mem_first_test.TrainPressure = pres_set_left;
            }
#if 00 && (AKY_DEBUG_ENABLE == 1 )
    s_mem_last = s_mem_first_test;
    flag_retest_bp_done = 1;
    flag_retest_bp = 1;

#endif

            if (flag_retest_bp == 1)
            {
                flag_retest_bp_done = 1;
                if (flag_manual_pre_adapt == 1)
                {
                    run_time_mem = get_mode_time_max(pre_adapt_mode_sel);
                }
                else
                {
                    run_time_mem = AUTO_TIME_MAX;
                }
                memset((uint8_t *)&g_s_mem,0,sizeof(s_mem_t));
                g_s_mem = s_mem_first_test;

                g_s_mem.s_train_time.min = run_time_mem/60;
                g_s_mem.s_train_time.secon = run_time_mem%60;
                g_s_mem.flag_ful_dat = 1;
                g_s_mem.s_left_bp_start = s_mem_last.s_left_bp;
                g_s_mem.s_right_bp_start = s_mem_last.s_right_bp;
                
                insert_one_mem(g_s_mem,g_e_user);
                syn_mem_to_net_control(1);
            }


            if(s_aky_task_0.pres_sys > MAX_SYS)
            {
                err_code = 0x03;
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("s_aky_task_0.pres_sys > MAX_SYS \n");
                break;
            }
            else if(s_aky_task_0.pres_sys < MIN_SYS)
            {
                err_code = 0x03;
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("s_aky_task_0.pres_sys < MIN_SYS \n");
                break;
            }

            if(s_aky_task_0.pres_dia > MAX_DIA)
            {
                err_code = 0x03;
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("s_aky_task_0.pres_dia > MAX_DIA \n");
                break;
            }
            else if(s_aky_task_0.pres_dia < MIN_DIA)
            {
                err_code = 0x03;
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("s_aky_task_0.pres_dia < MIN_DIA \n");
                break;
            }


            if(s_aky_task_0.pulse > MAX_PUL)
            {
                err_code = 0x04;
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("s_aky_task_0.pulse > MAX_PUL \n");
                break;
            }
            else if(s_aky_task_0.pulse < MIN_PUL)
            {
                err_code = 0x0a;
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("s_aky_task_0.pulse < MIN_PUL \n");
                break;
            }



            if(s_aky_task_1.pres_sys > MAX_SYS)
            {
                err_code = 0x03;
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("s_aky_task_1.pres_sys > MAX_SYS \n");
                break;
            }
            else if(s_aky_task_1.pres_sys < MIN_SYS)
            {
                err_code = 0x03;
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("s_aky_task_1.pres_sys < MIN_SYS \n");
                break;
            }

            if(s_aky_task_1.pres_dia > MAX_DIA)
            {
                err_code = 0x03;
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("s_aky_task_1.pres_dia > MAX_DIA \n");
                break;
            }
            else if(s_aky_task_1.pres_dia < MIN_DIA)
            {
                err_code = 0x03;
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("s_aky_task_1.pres_dia < MIN_DIA \n");
                break;
            }


            if(s_aky_task_1.pulse > MAX_PUL)
            {
                err_code = 0x04;
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("s_aky_task_1.pulse > MAX_PUL \n");
                break;
            }
            else if(s_aky_task_1.pulse < MIN_PUL)
            {
                err_code = 0x0a;
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("s_aky_task_1.pulse < MIN_PUL \n");
                break;
            }


            label_set_stat(2);
            lv_label_set_text_fmt(label_left_pres,"左侧:  %d/%dmmHg",s_aky_task_0.pres_sys,s_aky_task_0.pres_dia);
            lv_label_set_text_fmt(label_right_pres,"右侧:  %d/%dmmHg",s_aky_task_1.pres_sys,s_aky_task_1.pres_dia);
            lv_label_set_text_fmt(label_heart_rate,"心率:  %d次/分钟",s_aky_task_0.pulse);
            if (flag_retest_bp == 1)
            {
                lv_obj_set_hidden(img_test_stop, false);
            }

            Queue_Clear(&qVoice);
            if (flag_retest_bp == 1)
            {
                Queue_Push(&qVoice, PREADAP_END); 
            }
            else
            {
                Queue_Push(&qVoice, MEASURE_COMPLET); 
            }
            inner_voice_combine_cmd(INNER_VOICE_PRES_UNIT_MMG, s_aky_task_0.pres_sys, s_aky_task_0.pres_dia,s_aky_task_1.pres_sys,s_aky_task_1.pres_dia, s_aky_task_0.pulse);
            uint8_t who_level = get_bar_level_by_sys_dia(s_aky_task_0.pres_sys, s_aky_task_0.pres_dia);


            inner_voice_who(who_level);
            if (get_mem_5_days_high(g_e_user) == 1)
            {
                Queue_Push(&qVoice, BP_HIGH_SEEK_MEDICAL_ATTENTION_FAST); 
            }

            if (flag_retest_bp == 1)
            {
                g_s_mem.s_time = g_s_time;
                if (get_last_mem_gap_day(g_e_user,g_s_mem) > 2)
                {
                    Queue_Push(&qVoice, TRAIN_AVERAGE); 
                }
                else
                {
                    if (down_cnt_left > 3 || down_cnt_right > 3)
                    {
                        //多次触发重复加压
                        Queue_Push(&qVoice, TRAIN_GOOD); 
                    }
                    else
                    {
                        Queue_Push(&qVoice, TRAIN_GREAT); 
                    }
                }
                Queue_Push(&qVoice, DATA_SENDING); 

            }



            voice_play_array();

            u32_tick_last = g_u32_tick_cnt;

        }
        else if (step_sub == 1)
        {
            if (get_voice_busy_stat() == 1)
            {
                u32_tick_last = g_u32_tick_cnt;
            }
            if (g_u32_tick_cnt - u32_tick_last > 2000 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                if (g_flag_emc_mode == 1)
                {
                    step_sub = 21;
                }
                else
                {
                    if (flag_retest_bp == 0)
                    {
                        e_auto_test_stat = STAT_PRE_ADAPT;
                    }
                    else
                    {
                        step_sub = 20;
                    }
                }
            }


        }
        else if (step_sub == 20)
        {
            if (flag_retest_bp == 1)
            {
                
            }
            if (get_send_end_stat() != 0)
            {
                if (get_send_end_stat() == 1)
                {
                    voice_play_single(DATA_SEND_SUCC);
                }
                else if (get_send_end_stat() == 2)
                {
                    voice_play_single(DATA_SEND_ERR);
                }
                step_sub++;
                u32_tick_last = g_u32_tick_cnt;
            }
        }
        else if (step_sub == 21)
        {
            if (get_voice_busy_stat() == 1)
            {
                u32_tick_last = g_u32_tick_cnt;
            }
            if (g_u32_tick_cnt - u32_tick_last > 2000 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                if (g_flag_emc_mode == 1)
                {
                    flag_start_test = 1;
                    flag_test_stop = 0;
                    flag_retest_bp = 0;
                    flag_retest_bp_done = 0;
                    e_auto_test_stat = STAT_CAL_ZERO;
                    flag_start_play_avi = 0;
                    flag_disp_bar = 0;
                    video_index = 0;
                    flag_one_stop_trig = 0;
                    e_auto_test_stat = STAT_CAL_ZERO;
                    step_sub = 0;
                    test_time_max = 0;
                }
            }

        }
        

        
        break;
    } 
    case STAT_PRE_ADAPT:
    {
        if (step_sub == 0)
        {
            step_sub++;
            if (flag_retest_bp == 0)
            {
                if (s_mem_first_test.s_left_bp.sys >= 170 || s_mem_first_test.s_right_bp.sys  >= 170)
                {
                    e_auto_test_stat = STAT_HIGHT_BP;
                    // lv_obj_set_hidden(img_notice_high_bp, false);
                }
            }
            if (e_auto_test_stat == STAT_HIGHT_BP && flag_retest_bp == 0)
            {
                break;
            }
            label_set_stat(3);
            voice_play_single(PREADAP_START);
            if (flag_manual_pre_adapt == 1)
            {
                pres_set_left = get_mode_set_pres(pre_adapt_mode_sel);
                pres_set_right = get_mode_set_pres(pre_adapt_mode_sel);
                lv_label_set_text_fmt(label_left_pres,"设定压力:  %dmmHg",pres_set_left);
                lv_label_set_text_fmt(label_right_set_pres,"设定压力:  %dmmHg",pres_set_right);
                lv_label_set_text_fmt(label_heart_rate,"剩余时间:  %d分钟%02d秒",get_mode_run_time(pre_adapt_mode_sel)/60,get_mode_run_time(pre_adapt_mode_sel)%60);
                flag_update_right = 0;
                flag_update_left = 0;
           }
            else
            {
                pres_set_left = pres_sys_last + 50;
                pres_set_right = pres_sys_last2 + 50;
                if (pres_set_left != pres_set_right)
                {
                    if(pres_set_left > pres_set_right)
                    {
                        pres_set_right = pres_set_left;
                        flag_update_right = 1;
                    }
                    else if(pres_set_left < pres_set_right)
                    {
                        pres_set_left = pres_set_right;
                        flag_update_left = 1;
                    }
                }
                lv_label_set_text_fmt(label_left_pres,"设定压力:  %dmmHg",pres_set_left);
                lv_label_set_text_fmt(label_right_set_pres,"设定压力:  %dmmHg",pres_set_right);

                lv_label_set_text_fmt(label_heart_rate,"剩余时间:  %d分钟%02d秒",AUTO_RUN_TIME/60,AUTO_RUN_TIME%60);
            }
            lv_label_set_text_fmt(label_right_pres,"实时压力:  %dmmHg",0);
            lv_label_set_text_fmt(label_right_pres_2,"实时压力:  %dmmHg",0);

            down_detect(0);
            u32_tick_last = g_u32_tick_cnt;

            send_cnt = 0;
            cuff_err_detect_init();
            left_show_pres_last = 0;
            right_show_pres_last = 0;
            test_total_run_time_last = 0;

        } 
        else if (step_sub == 1)
        {
            if (g_u32_tick_cnt - u32_tick_last > 300 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;

                if (get_voice_busy_stat() == 0)
                {
                    if (flag_manual_pre_adapt == 1)
                    {
                        Task0_BP.PER_Mode = pre_adapt_mode_sel;
                        Task1_BP.PER_Mode = pre_adapt_mode_sel;            
                        Task0_BP.pre_hold_time = get_mode_step_time(pre_adapt_mode_sel) / 60;
                        Task1_BP.pre_hold_time = get_mode_step_time(pre_adapt_mode_sel) / 60;            
                    }
                    else
                    {
                        Task0_BP.PER_Mode = 5;
                        Task1_BP.PER_Mode = 5;            
                        Task0_BP.pre_hold_time = get_mode_step_time(4) / 60;
                        Task1_BP.pre_hold_time = get_mode_step_time(4) / 60;  
                        Task0_BP.pre_keep_pres = pres_set_left;         
                        Task1_BP.pre_keep_pres = pres_set_right;         
                    }
#if (DEBUG_PREADAP == 1)
                    if (flag_manual_pre_adapt == 0)
                    {
                        Task0_BP.PER_Mode = 2;
                        Task1_BP.PER_Mode = 2;            
                        Task0_BP.pre_hold_time = get_mode_step_time(2) / 60;
                        Task1_BP.pre_hold_time = get_mode_step_time(2) / 60;            
                    }
#endif
#if (DEBUG_PREADAP_SKIP == 1)
    step_sub = 6;
    break;
#endif

                    aky_task_0_send_cmd(TASK0_PER_PREPARE_Command);
                    aky_task_1_send_cmd(TASK1_PER_PREPARE_Command);
                    send_cnt++;
                    if (send_cnt >= 40)
                    {
                        send_cnt = 0;
                        e_auto_test_stat = STAT_SHOW_ERR;
                        err_code = 1;
                        printf("TASK0_PER_PREPARE_Command send_cnt >= 40\r\n");
                    }

                }
            }
            if (s_aky_task_0.e_aky_task_0_stat == TASK_0_STAT_PER_WAIT && s_aky_task_1.e_aky_task_1_stat == TASK_1_STAT_PER_WAIT)
            {
                //等待血压主控归0结束
                step_sub = 4;
                u32_tick_last = g_u32_tick_cnt;
                send_cnt = 0;
            }

            fil_cnt = 0;
        }
        // else if (step_sub == 2)
        // {
        //     if (g_u32_tick_cnt - u32_tick_last > 300 / 10)
        //     {
        //         u32_tick_last = g_u32_tick_cnt;

        //         if (s_aky_task_0.e_aky_task_0_stat == TASK_0_STAT_PER_WAIT && s_aky_task_1.e_aky_task_1_stat == TASK_1_STAT_PER_WAIT)
        //         {
        //             //等待血压主控归0结束
        //             step_sub = 4;
        //             u32_tick_last = g_u32_tick_cnt;
        //             send_cnt = 0;
        //         }
        //     }
        //     fil_cnt = 0;
        // }
        else if (step_sub == 4)
        {
            if (g_u32_tick_cnt - u32_tick_last > 300 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                aky_task_0_send_cmd(TASK0_PER_START_Command);
                aky_task_1_send_cmd(TASK1_PER_START_Command);
                send_cnt++;
                if (send_cnt >= 3)
                {
                    send_cnt = 0;
                    step_sub = 5;
                }
            }
        }
        else if (step_sub == 5)
        {
            step_sub = 6;
            fil_cnt2 =0;
            send_cnt = 0;
            send_cnt2 = 0;
            if (flag_update_left == 1 || flag_update_right == 1)
            {
                flag_update_onece = 1;
            }
            else
            {
                flag_update_onece = 0;
            }
            flag_start_update = 0;
            tran_step = 1;
        }
        else if (step_sub == 6)
        {
            if (g_u32_tick_cnt - u32_tick_last2 > 1000 / 10)
            {
                u32_tick_last2 = g_u32_tick_cnt;
                if(left_show_pres_last != Task0_BP.pres_mmg)
                {
                    left_show_pres_last = Task0_BP.pres_mmg;
                    lv_label_set_text_fmt(label_right_pres,"实时压力:  %dmmHg",left_show_pres_last);
                }
                if(right_show_pres_last != s_aky_task_1.pres_mmg)
                {
                    right_show_pres_last = s_aky_task_1.pres_mmg;
                    lv_label_set_text_fmt(label_right_pres_2,"实时压力:  %dmmHg",right_show_pres_last);
                }
                #if (DEBUG_PREADAP_SKIP == 1)
                if (test_total_run_time > 25)
                {
                    test_total_run_time -= 20;
                }
                else
                {
                    if (test_total_run_time > 0)
                    {
                        test_total_run_time -= 1;
                    }
                    
                }
                #endif
            }
            #if (DEBUG_PREADAP_SKIP == 0)
            test_total_run_time = Task0_BP.PER_Alltime;
            #endif
            if (test_total_run_time_last != test_total_run_time)
            {
                test_total_run_time_last = test_total_run_time;
                lv_label_set_text_fmt(label_heart_rate,"剩余时间:  %d分钟%02d秒",test_total_run_time_last/60,test_total_run_time_last%60);
            }

            if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;


                if (test_total_run_time <= 1)
                {
                    fil_cnt++;
                    if (fil_cnt >= 3000 / 500)
                    {
                        fil_cnt = 0;
                        e_auto_test_stat = STAT_RETEST_BP;
                    }
                }
                else
                {
                    fil_cnt = 0;
                }

                // if (flag_update_onece == 1)
                // {
                //     if (Task0_BP.PER_PumpStatus == 0 && Task1_BP.PER_PumpStatus == 0)
                //     {
                //         //d等待空闲时再同步气压
                //         fil_cnt2++;
                //         if (fil_cnt2 >= 5000 / 500)
                //         {
                //             fil_cnt2 = 0;
                //             flag_start_update = 1;
                //         }    
                //     }
                //     else
                //     {
                //         fil_cnt2 = 0;
                //     }

                //     if (flag_start_update == 1)
                //     {
                //         Task0_BP.pre_set = pres_set_left;
                //         if (flag_update_left == 1)
                //         {
                //             aky_task_0_send_cmd(TASK0_PER_UP);
                //         }
                        
                //         Task1_BP.pre_set = pres_set_right;
                //         if (flag_update_right == 1)
                //         {
                //             aky_task_1_send_cmd(TASK1_PER_UP);                
                //         }
                        
                //         send_cnt2++;
                //         if (send_cnt2 >= 3)
                //         {
                //             send_cnt2 = 0;
                //             flag_update_onece = 0;
                //             flag_start_update = 0;
                //             fil_cnt2 = 0;
                //         }
                //     }

                    
                // }
                #if (DEBUG_PREADAP_SKIP == 0)
                test_total_run_time = Task0_BP.PER_Alltime;
                #endif
    
                down_detect(1);

                if (Task0_BP.flag_step_end == 1)
                {
                    tran_step = Task0_BP.step_code;
                    Task1_BP.step_code = Task0_BP.step_code;
                    aky_task_1_send_cmd(TASK1_BP_NEXT_STEP);
                    send_cnt++;
                    if (send_cnt >= 10)
                    {
                        send_cnt = 0;
                        Task0_BP.flag_step_end = 0;
                    }
                    if (Task1_BP.flag_reply == 1)
                    {
                        Task1_BP.flag_reply = 0;
                        Task0_BP.flag_step_end = 0;
                    }
                }
                else
                {
                    send_cnt = 0;
                    Task1_BP.flag_reply = 0;
                }
                
            }
            #if (DEBUG_PREADAP_SKIP == 0)
            if (get_cuff_err(pres_set_left,pres_set_right,tran_step))
            {
                e_auto_test_stat = STAT_SHOW_ERR;
                err_code = 0x05;
                printf("get_cuff_err\n");
            }
            #endif

            
            
            
            if (s_aky_task_0.e_aky_task_0_stat == TASK_0_STAT_PER_WAIT && s_aky_task_1.e_aky_task_1_stat == TASK_1_STAT_PER_WAIT)
            {
                e_auto_test_stat = STAT_RETEST_BP;
            }
            
            if (s_aky_task_0.e_aky_task_0_stat == TASK_0_STAT_PER_FAIL || s_aky_task_1.e_aky_task_1_stat == TASK_1_STAT_PER_FAIL)
            {
                e_auto_test_stat = STAT_SHOW_ERR;
                printf("STAT_PRE_ADAPT -> STAT_SHOW_ERR\n");

                if (s_aky_task_0.e_aky_task_0_stat == TASK_0_STAT_PER_FAIL)
                {
                    err_code = s_aky_task_0.ERRORCode;
                    printf("TASK_0_STAT_PER_FAIL err_code =0x%02d\n",err_code);
                }
                else if (s_aky_task_1.e_aky_task_1_stat == TASK_1_STAT_PER_FAIL)
                {
                    err_code = s_aky_task_1.ERRORCode;
                    printf("TASK_1_STAT_PER_FAIL err_code =0x%02d\n",err_code);
                }
            }
        }
        break;
    }     
    case STAT_RETEST_BP:
    {
        if (step_sub == 0)
        {
            step_sub++;
            video_stop();

            label_set_stat(4);
            // if (flag_manual_pre_adapt == 1)
            // {
            //     test_total_run_time = get_mode_step_time(pre_adapt_mode_sel);
            // }
            // else
            // {
            //     test_total_run_time = AUTO_STEP_TIME;
            // }
            test_total_run_time = 60 * 5;

            aky_task_0_send_cmd(TASK0_BP_STOP_Command);
            aky_task_1_send_cmd(TASK1_BP_STOP_Command);
            flag_retest_bp = 1;
            flag_start_test = 1;
            voice_play_single(VOICE_RETEST_BP);


        }
        else if (step_sub == 1)
        {
            if (second_last != g_s_time.secon)
            {
                second_last = g_s_time.secon;
                #if 00 && (AKY_DEBUG_ENABLE == 1)
                if (test_total_run_time > 15)
                {
                    test_total_run_time-=10;
                }
                else
                {
                    test_total_run_time-=1;
                }
                #else
                test_total_run_time--;
                #endif
                lv_label_set_text_fmt(label_heart_rate,"剩余时间:  %d分钟%d秒",test_total_run_time/60,test_total_run_time%60);
                if (test_total_run_time <= 0)
                {
                    e_auto_test_stat = STAT_CAL_ZERO;
                    test_time_max = 0;
                }
            }
        }
        break;
    }    
    case STAT_SHOW_ERR:
    {
        if (step_sub == 0)
        {
            step_sub++;
            video_stop();
            label_set_stat(6);
            flag_test_stop = 1;
            voice_stop();
            voice_exit_bg_play();
            show_err_code_detail(err_code);
            aky_task_0_send_cmd(TASK0_BP_STOP_Command);
            aky_task_1_send_cmd(TASK1_BP_STOP_Command);
            if (s_mem_first_test.s_left_bp.sys != 0)
            {
               if (flag_retest_bp == 1)
                {
                    if (flag_manual_pre_adapt == 1)
                    {
                        run_time_mem = get_mode_time_max(pre_adapt_mode_sel);
                    }
                    else
                    {
                        run_time_mem = AUTO_TIME_MAX;
                    }
                }
                else
                {
                    if (flag_manual_pre_adapt == 1)
                    {
                        run_time_mem = (get_mode_run_time(pre_adapt_mode_sel) - test_total_run_time);
                    }
                    else
                    {
                        run_time_mem = (AUTO_RUN_TIME - test_total_run_time);
                    }
                }
                memset((uint8_t *)&g_s_mem,0,sizeof(s_mem_t));
                g_s_mem = s_mem_first_test;

                g_s_mem.s_train_time.min = run_time_mem/60;
                g_s_mem.s_train_time.secon = run_time_mem%60;
                insert_one_mem(g_s_mem,g_e_user);
                syn_mem_to_net_control(1);
            }



            send_cnt = 0;

        }
        else if (step_sub == 1)
        {
            if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                aky_task_0_send_cmd(TASK0_BP_STOP_Command);
                aky_task_1_send_cmd(TASK1_BP_STOP_Command);
                send_cnt++;
                if (send_cnt >= 3)
                {
                    send_cnt = 0;
                    step_sub = 2;
                }
    
            }

        }
        break;
    }    
    case STAT_HIGHT_BP:
    {
        if (step_sub == 0)
        {
            step_sub++;
            label_set_stat(7);
            flag_test_stop = 1;
            voice_stop();
            voice_exit_bg_play();
            
            {
                {
                    run_time_mem = 0;

                    memset((uint8_t *)&g_s_mem,0,sizeof(s_mem_t));
                    g_s_mem = s_mem_first_test;

                    g_s_mem.s_train_time.min = run_time_mem/60;
                    g_s_mem.s_train_time.secon = run_time_mem%60;
                    insert_one_mem(g_s_mem,g_e_user);
                    syn_mem_to_net_control(1);
                }
            }

            send_cnt = 0;
            aky_task_0_send_cmd(TASK0_BP_STOP_Command);
            aky_task_1_send_cmd(TASK1_BP_STOP_Command);

        }
        else if (step_sub == 1)
        {
            if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                aky_task_0_send_cmd(TASK0_BP_STOP_Command);
                aky_task_1_send_cmd(TASK1_BP_STOP_Command);
                send_cnt++;
                if (send_cnt >= 3)
                {
                    send_cnt = 0;
                    step_sub = 2;
                }
    
            }


        }
        break;
    }    

    case STAT_MANUAL_STOP:
    {
        if (step_sub == 0)
        {
            step_sub++;
            video_stop();

            label_set_stat(5);
            voice_stop();
            voice_exit_bg_play();
            aky_task_0_send_cmd(TASK0_BP_STOP_Command);
            aky_task_1_send_cmd(TASK1_BP_STOP_Command);
            if (g_flag_emc_mode == 1)
            {
                //返回工程界面
                g_flag_emc_mode = 0;
                synwit_ui_load_screen(SCREEN012);
                break;
            }
            if ((pres_sys_last == 0 || test_total_run_time == 0) && flag_retest_bp == 0)
            {
                lv_label_set_text_fmt(label_left_pres,"设定压力:  ");
                lv_label_set_text_fmt(label_right_pres,"实时压力:  ");
                lv_label_set_text_fmt(label_heart_rate,"剩余时间:  ");
            }
            else
            {
                if (flag_manual_pre_adapt == 1)
                {
                    lv_label_set_text_fmt(label_left_pres,"设定压力:  %dmmHg",get_mode_set_pres(pre_adapt_mode_sel));
                }
                else
                {
                    lv_label_set_text_fmt(label_left_pres,"设定压力:  %dmmHg",pres_set_left);
                }
                // lv_label_set_text_fmt(label_right_pres,"实时压力:  %dmmHg",Task0_BP.pres_mmg);
                lv_label_set_text_fmt(label_right_pres,"实时压力:  %dmmHg",0);

                lv_label_set_text_fmt(label_heart_rate,"剩余时间:  %d分钟%d秒",test_total_run_time/60,test_total_run_time%60);
            //    if (flag_retest_bp == 1 || (AUTO_RUN_TIME - test_total_run_time) > 60)
               if (flag_retest_bp == 1 || s_mem_first_test.s_left_bp.sys != 0)
                {
                    if (flag_retest_bp == 1)
                    {
                        if (flag_manual_pre_adapt == 1)
                        {
                            run_time_mem = get_mode_time_max(pre_adapt_mode_sel) - test_total_run_time;
                        }
                        else
                        {
                            run_time_mem = AUTO_TIME_MAX - test_total_run_time;
                        }
                    }
                    else
                    {
                        if (flag_manual_pre_adapt == 1)
                        {
                            run_time_mem = (get_mode_run_time(pre_adapt_mode_sel) - test_total_run_time);
                        }
                        else
                        {
                            run_time_mem = (AUTO_RUN_TIME - test_total_run_time);
                        }
                    }
                    memset((uint8_t *)&g_s_mem,0,sizeof(s_mem_t));
                    g_s_mem = s_mem_first_test;

                    g_s_mem.s_train_time.min = run_time_mem/60;
                    g_s_mem.s_train_time.secon = run_time_mem%60;
                    insert_one_mem(g_s_mem,g_e_user);
                    syn_mem_to_net_control(1);
                }
            }
            send_cnt = 0;
        }
        else if (step_sub == 1)
        {
            if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                aky_task_0_send_cmd(TASK0_BP_STOP_Command);
                aky_task_1_send_cmd(TASK1_BP_STOP_Command);
                send_cnt++;
                if (send_cnt >= 3)
                {
                    send_cnt = 0;
                    step_sub = 2;
                }
            }
            
        }
        break;
    }    
    default:
        break;
    }
}

void label_set_stat(uint8_t stat)
{
    lv_obj_set_hidden(label_title,true);
    lv_obj_set_hidden(label_left_pres,true);
    lv_obj_set_hidden(label_right_pres,true);
    lv_obj_set_hidden(label_heart_rate, true);
    lv_obj_set_hidden(label_right_pres_2, true);
    lv_obj_set_hidden(label_title2, true);
    lv_obj_set_hidden(img_test_stop, true);
    lv_obj_set_hidden(img_notice_high_bp, true);
    lv_obj_set_hidden(label_right_set_pres, true);
    lv_obj_set_hidden(label_tran_title, true);
    lv_obj_set_hidden(label_video, true);
    if (stat == 0)
    {
        //全部隐藏 初始状态
        lv_obj_set_hidden(tc_area_ready, false);
        lv_obj_set_hidden(tc_area_back, false);

    }
    else if (stat == 1)
    {
        //测量血压 
        lv_obj_set_hidden(label_title,false);
        lv_obj_set_hidden(label_left_pres,false);
        lv_obj_set_hidden(label_right_pres,false);
        lv_obj_set_hidden(tc_area_ready, true);
        lv_obj_set_hidden(tc_area_back, true);

        lv_obj_set_size(label_title,880,50);
        lv_obj_set_pos(label_title,106,218);
        lv_label_set_text_fmt(label_title,"正在测量血压, 请保持安静, 并尽量伸直双上肢");

        lv_obj_set_size(label_left_pres,230,50);
        lv_obj_set_pos(label_left_pres,222,355);
        lv_label_set_text_fmt(label_left_pres,"左侧:  0");

        lv_obj_set_size(label_right_pres,230,50);
        lv_obj_set_pos(label_right_pres,616,355);
        lv_label_set_text_fmt(label_right_pres,"右侧:  0");

    }
    else if (stat == 2)
    {
        //血压结果 
        lv_obj_set_hidden(label_title,false);
        lv_obj_set_hidden(label_left_pres,false);
        lv_obj_set_hidden(label_right_pres,false);
        lv_obj_set_hidden(label_heart_rate, false);
        lv_obj_set_hidden(tc_area_ready, true);
        lv_obj_set_hidden(tc_area_back, true);

        lv_obj_set_size(label_title,150,50);
        lv_obj_set_pos(label_title,248,140);
        lv_label_set_text_fmt(label_title,"血压");

        lv_obj_set_size(label_left_pres,400,50);
        lv_obj_set_pos(label_left_pres,370,205);
        lv_label_set_text_fmt(label_left_pres,"左侧:  ");

        lv_obj_set_size(label_right_pres,400,50);
        lv_obj_set_pos(label_right_pres,370,280);
        lv_label_set_text_fmt(label_right_pres,"右侧:  ");

        lv_obj_set_size(label_heart_rate,330,50);
        lv_obj_set_pos(label_heart_rate,247,350);
        lv_label_set_text_fmt(label_heart_rate,"心率:  ");

    }
    else if (stat == 3)
    {
        //预适应 
        lv_obj_set_hidden(label_left_pres,false);
        lv_obj_set_hidden(label_right_pres,false);
        lv_obj_set_hidden(label_heart_rate, false);
        lv_obj_set_hidden(label_title2, false);
        lv_obj_set_hidden(label_right_pres_2, false);
        lv_obj_set_hidden(label_right_set_pres, false);
        lv_obj_set_hidden(label_tran_title, false);
        lv_obj_set_hidden(label_video, false);

        lv_obj_set_pos(label_title2,435,108);
        lv_label_set_text_fmt(label_title2,"训练中...");

        lv_obj_set_size(label_left_pres,400,50);
        lv_obj_set_pos(label_left_pres,98,256);
        lv_label_set_text_fmt(label_left_pres,"设定压力:  ");

        lv_obj_set_size(label_right_pres,400,50);
        lv_obj_set_pos(label_right_pres,98,330);
        lv_label_set_text_fmt(label_right_pres,"实时压力:  ");

        lv_obj_set_size(label_right_set_pres,400,50);
        lv_obj_set_pos(label_right_set_pres,585,256);
        lv_label_set_text_fmt(label_right_set_pres,"设定压力:  ");

        lv_obj_set_size(label_right_pres_2,400,50);
        lv_obj_set_pos(label_right_pres_2,585,330);
        lv_label_set_text_fmt(label_right_pres_2,"实时压力:  ");

        lv_obj_set_size(label_heart_rate,460,50);
        lv_obj_set_pos(label_heart_rate,98,415);
        lv_label_set_text_fmt(label_heart_rate,"剩余时间:  ");

    }
    else if (stat == 4)
    {
        //预适应 再次测量血压
        lv_obj_set_hidden(label_left_pres,false);
        lv_obj_set_hidden(label_right_pres,false);
        lv_obj_set_hidden(label_heart_rate, false);

        lv_obj_set_size(label_left_pres,600,50);
        lv_obj_set_pos(label_left_pres,256,208);
        lv_label_set_text_fmt(label_left_pres,"请您保持安静,  倒计时结束后将");

        lv_obj_set_size(label_right_pres,400,50);
        lv_obj_set_pos(label_right_pres,336,264);
        lv_label_set_text_fmt(label_right_pres,"为您再次测量血压");

        lv_obj_set_size(label_heart_rate,460,50);
        lv_obj_set_pos(label_heart_rate,336,384);
        lv_label_set_text_fmt(label_heart_rate,"剩余时间:  ");

    }
    else if (stat == 5)
    {
        //训练中断
        lv_obj_set_hidden(label_left_pres,false);
        lv_obj_set_hidden(label_right_pres,false);
        lv_obj_set_hidden(label_heart_rate, false);
        lv_obj_set_hidden(label_title2, false);
        lv_obj_set_hidden(img_test_stop, false);


        lv_obj_set_pos(label_title2,336,150);
        lv_label_set_text_fmt(label_title2,"训练中断");

        lv_obj_set_size(label_left_pres,400,50);
        lv_obj_set_pos(label_left_pres,336,250);
        lv_label_set_text_fmt(label_left_pres,"设定压力:  ");

        lv_obj_set_size(label_right_pres,400,50);
        lv_obj_set_pos(label_right_pres,336,317);
        lv_label_set_text_fmt(label_right_pres,"实时压力:  ");

        // lv_obj_set_size(label_right_pres_2,400,50);
        // lv_obj_set_pos(label_right_pres_2,336,360);
        // lv_label_set_text_fmt(label_right_pres_2,"右侧压力:  ");

        lv_obj_set_size(label_heart_rate,460,50);
        lv_obj_set_pos(label_heart_rate,336,390);
        lv_label_set_text_fmt(label_heart_rate,"剩余时间:  ");

    }
    else if (stat == 6)
    {
        //报错
        lv_obj_set_hidden(label_left_pres,false);
        lv_obj_set_hidden(label_right_pres,false);
        lv_obj_set_hidden(img_test_stop, false);

        lv_obj_set_size(label_left_pres,600,50);
        lv_obj_set_pos(label_left_pres,256,208);


        lv_label_set_long_mode(label_right_pres, LV_LABEL_LONG_BREAK);
        lv_obj_set_size(label_right_pres,600,150);
        lv_obj_set_pos(label_right_pres,256,264);

        lv_label_set_text_fmt(label_left_pres,"ERR");
        lv_label_set_text_fmt(label_right_pres," ");
    }
    else if (stat == 7)
    {
        //当前血压不适宜进行训练
        lv_obj_set_hidden(img_notice_high_bp, false);

    }

}
//自动训练模式
#define MODE_1_TIME_MAX (60 * 32)//最大时间
#define MODE_1_STEP_TIME (60 * 3)//步骤时间
#define MODE_1_RUN_TIME (60 * 27)//循环时间 

#define MODE_2_TIME_MAX (60 * 50)//最大时间
#define MODE_2_STEP_TIME (60 * 5)//步骤时间
#define MODE_2_RUN_TIME (MODE_2_TIME_MAX - MODE_2_STEP_TIME)//循环时间 

#define MODE_3_TIME_MAX (60 * 50)//最大时间
#define MODE_3_STEP_TIME (60 * 5)//步骤时间
#define MODE_3_RUN_TIME (MODE_3_TIME_MAX - MODE_3_STEP_TIME)//循环时间 

#define MODE_4_TIME_MAX (60 * 50)//最大时间
#define MODE_4_STEP_TIME (60 * 5)//步骤时间
#define MODE_4_RUN_TIME (MODE_4_TIME_MAX - MODE_4_STEP_TIME)//循环时间 

uint16_t get_mode_time_max(uint8_t mode_sel)
{
    uint16_t res = 0;
    if (mode_sel == 1)
    {
        res = MODE_1_TIME_MAX;
    }
    else if (mode_sel == 2)
    {
        res = MODE_2_TIME_MAX;
    }
    else if (mode_sel == 3)
    {
        res = MODE_3_TIME_MAX;
    }
    else if (mode_sel == 4)
    {
        res = MODE_4_TIME_MAX;
    }
    return res;
}
uint16_t get_mode_step_time(uint8_t mode_sel)
{
    uint16_t res = 0;
    if (mode_sel == 1)
    {
        res = MODE_1_STEP_TIME;
    }
    else if (mode_sel == 2)
    {
        res = MODE_2_STEP_TIME;
    }
    else if (mode_sel == 3)
    {
        res = MODE_3_STEP_TIME;
    }
    else if (mode_sel == 4)
    {
        res = MODE_4_STEP_TIME;
    }
    return res;
}

uint16_t get_mode_run_time(uint8_t mode_sel)
{
    uint16_t res = 0;
    if (mode_sel == 1)
    {
        res = MODE_1_RUN_TIME;
    }
    else if (mode_sel == 2)
    {
        res = MODE_2_RUN_TIME;
    }
    else if (mode_sel == 3)
    {
        res = MODE_3_RUN_TIME;
    }
    else if (mode_sel == 4)
    {
        res = MODE_4_RUN_TIME;
    }
    return res;
}

uint16_t get_mode_set_pres(uint8_t mode_sel)
{
    uint16_t res = 0;
    if (mode_sel == 1)
    {
        res = 180;
    }
    else if (mode_sel == 2)
    {
        res = 180;
    }
    else if (mode_sel == 3)
    {
        res = 200;
    }
    else if (mode_sel == 4)
    {
        res = 220;
    }
    return res;
}

void show_err_code_detail(uint8_t err_code)
{
    if (err_code == 0x05)
    {
        lv_label_set_text_fmt(label_left_pres,"ERRU");
        lv_label_set_text_fmt(label_right_pres,"测量或训练时压力在一定时间内未达到设定值,15s内达不到30mmhg,1min达不到设定值");
    }
    else if (err_code == 0X01)
    {
        lv_label_set_text_fmt(label_left_pres,"ERR1");
        lv_label_set_text_fmt(label_right_pres,"测量时信号太弱");
    }
    else if (err_code == 0X02 || err_code == 0X06)
    {
        lv_label_set_text_fmt(label_left_pres,"ERR2");
        lv_label_set_text_fmt(label_right_pres,"测量时受到干扰");
    }
    else if (err_code == 0X03)
    {
        lv_label_set_text_fmt(label_left_pres,"ERR3");
        lv_label_set_text_fmt(label_right_pres,"测量结果错误");
    }
    else if (err_code == 0X04)
    {
        lv_label_set_text_fmt(label_left_pres,"ERR HI");
        lv_label_set_text_fmt(label_right_pres,"心率超上限");
    }
    else if (err_code == 0X0a)
    {
        lv_label_set_text_fmt(label_left_pres,"ERR LO");
        lv_label_set_text_fmt(label_right_pres,"心率超下限");
    }
    else if (err_code == 0X07)
    {
        lv_label_set_text_fmt(label_left_pres,"ERRH");
        lv_label_set_text_fmt(label_right_pres,"绑带气压过高，超压保护");
    }
    else
    {
        lv_label_set_text_fmt(label_left_pres,"ERR2");
        lv_label_set_text_fmt(label_right_pres,"测量时受到干扰");
        #if 00 && (AKY_DEBUG_ENABLE == 1)
        static char temp_buf[50] = {0};
        sprintf(temp_buf,"%s",lv_label_get_text(label_left_pres));
        lv_label_set_text_fmt(label_left_pres,"%s real err_code = 0x%02x",temp_buf,err_code);
        #endif
    }

}
void down_detect(uint8_t stat)
{
    static uint16_t pres_mmg_last;                     // 压力值
    static uint16_t pres_mmg_last_2;                     // 压力值
    static uint16_t fil_cnt_2;                     // 
    static uint16_t fil_cnt_3;                     // 
    static uint8_t flag_down;
    static uint16_t fil_cnt_4;                     // 
    static uint16_t fil_cnt_5;                     // 
    static uint16_t fil_cnt_6;                     // 
    static uint16_t fil_cnt_7;                     // 
    static uint8_t flag_down_2;

    if (stat == 0)
    {
        fil_cnt_4 = 0;
        fil_cnt_5 = 0;
        fil_cnt_2 = 0;
        fil_cnt_3 = 0;
        fil_cnt_6 = 0;
        fil_cnt_7 = 0;
        flag_down = 0;
        flag_down_2 = 0;
        down_cnt_left = 0;
        down_cnt_right = 0;
        pres_mmg_last = 0;
        pres_mmg_last_2 = 0;
    }
    else
    {
        if (pres_mmg_last_2 != Task1_BP.pres_mmg)
        {
            if (Task1_BP.pres_mmg > pres_mmg_last_2)
            {
                fil_cnt_4++;
                fil_cnt_5 = 0;
                fil_cnt_6 = 0;
                if (fil_cnt_4 > 10)
                {
                    fil_cnt_4 = 0;
                    flag_down_2 = 0;
                }
            }
            else if (Task1_BP.pres_mmg < pres_mmg_last_2)
            {
                fil_cnt_4 = 0;
                fil_cnt_5++;
                if (fil_cnt_5 > 10)
                {
                    fil_cnt_5 = 0;
                    if (Task1_BP.pres_mmg < 80)
                    {
                        if (flag_down_2 == 0)
                        {
                            if(Task1_BP.pres_mmg > 2)
                            {
                                fil_cnt_6++;
                                if (fil_cnt_6 > 5)
                                {
                                    fil_cnt_6 = 0;
                                    flag_down_2 = 1;
                                    down_cnt_right++;
                                }
                            }
                            else
                            {
                                fil_cnt_6 = 0;
                            }
                        }
                    }
                }
            }
            else
            {
                fil_cnt_4 = 0;
                fil_cnt_5 = 0;
            }
            pres_mmg_last_2 = Task1_BP.pres_mmg;
        }

        if (pres_mmg_last != Task0_BP.pres_mmg)
        {
            if (Task0_BP.pres_mmg > pres_mmg_last)
            {
                fil_cnt_2++;
                fil_cnt_3 = 0;
                if (fil_cnt_2 > 10)
                {
                    fil_cnt_2 = 0;
                    flag_down = 0;
                }
            }
            else if (Task0_BP.pres_mmg < pres_mmg_last)
            {
                fil_cnt_2 = 0;
                fil_cnt_3++;
                if (fil_cnt_3 > 10)
                {
                    fil_cnt_3 = 0;
                    if (Task0_BP.pres_mmg < 80)
                    {
                        if (flag_down == 0)
                        {
                            if(Task0_BP.pres_mmg > 2)
                            {
                                fil_cnt_7++;
                                if (fil_cnt_7 > 5)
                                {
                                    fil_cnt_7 = 0;
                                    flag_down = 1;
                                    down_cnt_left++;
                                }
                            }
                            else
                            {
                                fil_cnt_7 = 0;
                            }
                        }
                    }
                }
            }
            else
            {
                fil_cnt_2 = 0;
                fil_cnt_3 = 0;
            }
            pres_mmg_last = Task0_BP.pres_mmg;
        }


    }
}

static uint16_t fil_cnt = 0; 
static uint16_t fil_cnt1 = 0; 
void cuff_err_detect_init(void)
{
    fil_cnt = 0; 
    fil_cnt1 = 0;
}

//训练过程中检测臂带打气和泄气情况
//return 0:正常 1:错误
// tran_step 打气方向 单数:打气 双数:泄气
uint8_t get_cuff_err(uint16_t left_target,uint16_t right_target,uint16_t tran_step)
{
    uint8_t res = 0;
    uint8_t pump_dir = 0;
    static uint32_t tick_cnt_last;
    if (tran_step % 2)
    {
        pump_dir = 1;
    }
    else
    {
        pump_dir = 0;
    }
    if(g_u32_tick_cnt - tick_cnt_last >= 1000 / 10)
    {
        tick_cnt_last = g_u32_tick_cnt;
        if(pump_dir == 1)
        {
            if (Task0_BP.pres_mmg < (left_target / 2))
            {
                fil_cnt++;
                if (fil_cnt > 15)
                {
                    fil_cnt = 0;
                    res = 1;
                }
            }
            else
            {
                fil_cnt = 0;
            }
            if (Task1_BP.pres_mmg < (right_target / 2))
            {
                fil_cnt1++;
                if (fil_cnt1 > 15)
                {
                    fil_cnt1 = 0;
                    res = 1;
                }
            }
            else
            {
                fil_cnt1 = 0;
            }

        }
        else
        {
            if (Task0_BP.pres_mmg > 10)
            {
                fil_cnt++;
                if (fil_cnt > 25)
                {
                    fil_cnt = 0;
                    res = 1;
                }
            }
            else
            {
                fil_cnt = 0;
            }
            if (Task1_BP.pres_mmg > 10)
            {
                fil_cnt1++;
                if (fil_cnt1 > 25)
                {
                    fil_cnt1 = 0;
                    res = 1;
                }
            }
            else
            {
                fil_cnt1 = 0;
            }

        }
    }
    return res;
}

void video_set_show_stat(uint8_t stat)
{
    lv_obj_set_hidden(avi_2, true);
    lv_obj_set_hidden(image_close, true);
    lv_obj_set_hidden(touch_area_avi_1, true);
    lv_obj_set_hidden(touch_area_avi_2, true);
    lv_obj_set_hidden(touch_area_avi_3, true);
    lv_obj_set_hidden(touch_area_avi_4, true);
    lv_obj_set_hidden(tc_area_back_video, true);
    lv_obj_set_hidden(bar_indic_voice, true);
    lv_obj_set_hidden(bar_indic_bl, true);
    lv_obj_set_hidden(image_video_bg, true);
    if (stat == 0)
    {
        // lv_obj_set_hidden(tc_area_one_click_stop, false);
        

    }
    else if (stat == 1)
    {
        lv_obj_set_hidden(touch_area_avi_1, false);
        lv_obj_set_hidden(touch_area_avi_2, false);
        lv_obj_set_hidden(touch_area_avi_3, false);
        lv_obj_set_hidden(touch_area_avi_4, false);
        lv_obj_set_hidden(tc_area_back_video, false);
        lv_obj_set_hidden(image_video_bg, false);
        // lv_obj_set_hidden(tc_area_one_click_stop, true);
        flag_start_play_avi = 0;
        flag_one_stop_trig = 0;

        lv_obj_set_hidden(label_title,true);
        lv_obj_set_hidden(label_left_pres,true);
        lv_obj_set_hidden(label_right_pres,true);
        lv_obj_set_hidden(label_heart_rate, true);
        lv_obj_set_hidden(label_right_pres_2, true);
        lv_obj_set_hidden(label_title2, true);
        lv_obj_set_hidden(img_test_stop, true);
        lv_obj_set_hidden(img_notice_high_bp, true);
        lv_obj_set_hidden(label_right_set_pres, true);
        lv_obj_set_hidden(label_tran_title, true);
        lv_obj_set_hidden(label_video, true);
    
    }
     else if (stat == 3)
    {
        lv_obj_set_hidden(label_left_pres,false);
        lv_obj_set_hidden(label_right_pres,false);
        lv_obj_set_hidden(label_heart_rate, false);
        lv_obj_set_hidden(label_title2, false);
        lv_obj_set_hidden(label_right_pres_2, false);
        lv_obj_set_hidden(label_right_set_pres, false);
        lv_obj_set_hidden(label_tran_title, false);
        lv_obj_set_hidden(label_video, false);

    
    }
   
}

void video_stop(void)
{
    lv_avi_pause(avi_2);
	stop_harware_dac();
    lv_obj_set_hidden(avi_2, true);
    lv_obj_set_hidden(image_close, true);
    flag_start_play_avi = 0;
    flag_one_stop_trig = 0;
    flag_is_open_video = 0;
    lv_obj_set_click(avi_2,false);

    stat_bar_restart_flush();
    video_set_show_stat(0);
    lv_avi_stop(avi_2);

}
